package com.rem.elasticsearch.dao;

import com.rem.elasticsearch.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * @author Rem
 * @date 2021-06-03
 */
@Repository
public interface ProductDao extends ElasticsearchRepository<Product, Long> {

    /**
     * 根据字段查询分页 等同于
     * * @Query("{\"bool\" : {\"must\" : {\"field\" : {\"category\" : \"?\"}}}}")
     *
     * @param category
     * @param pageable
     * @return
     */
    Page<Product> findProductsByCategory(String category, Pageable pageable);

    /**
     * @param firstCode
     * @param pageable
     * @return
     */
    @Query("{\"bool\" : {\"must\" : {\"field\" : {\"firstCode.keyword\" : \"?\"}}}}")
    Page<Product> findByFirstCode(String firstCode, Pageable pageable);

    /**
     * @param secordCode
     * @param pageable
     * @return
     */
    @Query("{\"bool\" : {\"must\" : {\"field\" : {\"secordCode.keyword\" : \"?\"}}}}")
    Page<Product> findBySecordCode(String secordCode, Pageable pageable);
}
